import * as vscode from 'vscode';
import { fetchDocList } from '../api/fetchDocList';
import { fetchModuleList } from '../api/fetchModuleList';
import { fetchProjectList } from '../api/fetchProjectList';
import { fetchSpaceList } from '../api/fetchSpaceList';
import { getSensitiveInfo, getUsers, setSensitiveInfo } from '../configuration';
import { log } from '../helper/outputChannel';
import { Memory } from '../memory';
import { Aborter } from '../network/aborter';
import { addUser } from './addUser';

export async function fetchApiList() {
  const users = await getUsers();
  if (!users.length) { return await addUser(); }
  return Promise.all(users.map(user => fetchApiListByUser(user)));
}

const aborter = new Aborter();
export async function fetchApiListByUser(user: Memory.IUser) {
  log.show(true);
  aborter.abort();
  try {
    log.info('[TAV] 获取空间列表...');
    const spaceList = await fetchSpaceList(
      user.url + '/space/list',
      { authorization: user.token },
      void 0,
      aborter.get()
    );

    const spaces = Memory.addSpaceList(user, spaceList);
    log.info('[TAV] 获取空间列表成功, 获取项目列表...');
    const requests = spaceList.map(space => fetchProjectList(
      user.url + '/space/project/list',
      { authorization: user.token },
      { spaceId: space.id },
    ).then(list => {
      for (const item of list) {
        item.spaceId = space.id;
        item.spaceName = space.name;
      }
      return list;
    }), aborter.get());

    const projectList = (await Promise.all(requests)).flat();
    const projects = Memory.addProjectList(user, projectList);
    log.info('[TAV] 获取项目列表成功, 获取模块列表...');
    const requests1 = projectList.map(project => fetchModuleList(
      user.url + '/module/list',
      { authorization: user.token },
      { projectId: project.id },
    ).then(list => {
      for (const item of list) {
        item.spaceId = project.spaceId;
        item.spaceName = project.spaceName;
        item.projectId = project.id;
        item.projectName = project.name;
      }
      return list;
    }), aborter.get());

    const moduleList = (await Promise.all(requests1)).flat();
    const modules = Memory.addModuleList(user, moduleList);
    log.info('[TAV] 获取模块列表成功, 获取接口文档列表列表...');
    const requests2 = moduleList.map(module => fetchDocList(
      user.url + '/doc/list',
      { authorization: user.token },
      { moduleId: module.id },
    ).then(list => {
      for (const item of list) {
        item.spaceId = module.spaceId;
        item.spaceName = module.spaceName;
        item.projectId = module.projectId;
        item.projectName = module.projectName;
        item.moduleId = module.id;
        item.moduleName = module.name;
      }
      return list;
    }), aborter.get());

    const docList = (await Promise.all(requests2)).flat();

    const documents = Memory.addDocList(user, docList);

    const sensitiveInfo = await getSensitiveInfo();
    sensitiveInfo.apiInfo = {
      ...sensitiveInfo.apiInfo,
      [Memory.userKey(user)]: {
        spaces,
        projects,
        modules,
        documents
      }
    };
    await setSensitiveInfo(sensitiveInfo);
    log.info('[TAV] 获取接口文档列表成功');

  } catch (error: any) {
    error && vscode.window.showErrorMessage(error);
  } finally {
    aborter.clear();
  }
}